package com.angel.controller;

import com.angel.bean.*;
import com.angel.exception.StatusException;
import com.angel.service.*;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userInfoController")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private BabyInfoService babyInfoService;

    @Autowired
    private VipInfoService vipInfoService;

    @Autowired
    private VipCardService vipCardService;

    @Autowired
    private BranchesService branchesService;

    @Autowired
    private OtherlogService otherlogService;

    /**
     * 添加用户信息
     *
     * @return 重定向所有用户
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/addUserInfo")
    @ResponseBody
    public Object addUserInfo(UserInfo userInfo, VipInfo vipInfo) {
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        System.out.println("------------------------------------进入addUserInfo 控制层");
        int result = 0;
        userInfo.setState(1);
        result = userInfoService.insert(userInfo);
        vipInfo.setBranId(employee.getBranchesId());
        vipInfo.setUserId(userInfo.getId());
        if (result != 0) {
            result = vipInfoService.insert(vipInfo);
            if (result == 0) {
                userInfoService.delUserInfo(userInfo);
            }
        }
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (result != 0) {
            map.put("addRusult", "success");
        }
        return map;
    }

    /**
     * 查询单个用户信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneUser")
    @ResponseBody
    public Object findOneUser(UserInfo userInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        System.out.println("--------进入获得用户控制层---------------查询单个用户信息");
        UserInfo userp = userInfoService.findOne(userInfo);
        map.put("userp", userp);
        return map;
    }


    /**
     * 删除用户信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * 前端传入id
     */
    @RequiresPermissions("user")
    @RequestMapping("/deleteUserInfo")
    @ResponseBody
    public Object deleteUserInfo(UserInfo userInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        // 逻辑删除
        userInfo.setState(0);
        VipInfo vipInfo = new VipInfo();
        int result = 0;
        int result1 = 0;
        vipInfo.setUserId(userInfo.getId());
        result1 = vipInfoService.delVipInfo(vipInfo);
        result = userInfoService.delUserInfo(userInfo);
        if (result != 0 || result1 != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 更新数据
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * 前端传入id
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Object updateUserInfo(UserInfo userInfo, VipInfo vipInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        vipInfo.setUserId(userInfo.getId());
        vipInfo.setBranId(employee.getBranchesId());
        int result = userInfoService.updateUserInfo(userInfo,vipInfo);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }


    /**
     * 判断是否存在当前档案编号
     *
     * @param babyInfo
     * @return
     */
    @RequestMapping("/findOneBabyInfo")
    @ResponseBody
    public Object findOneBabyInfo(BabyInfo babyInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        System.out.println("---------进入查询宝宝控制层-------------------------");
        BabyInfo bb = babyInfoService.findOne(babyInfo);
        map.put("bb", bb);
        return map;
    }

    /**
     * 查询单个信息vidId
     *
     * @param vipInfo
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/findOneVipInfo")
    @ResponseBody
    public Object findOneVipInfo(VipInfo vipInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        System.out.println("---------进入查询vip控制层-----------------------");
        VipInfo vipinf = vipInfoService.findOne(vipInfo);
        map.put("vipinf", vipinf);
        return map;
    }

    //分页实现

    /**
     * 查询用户信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findUserInfo")
    @ExceptionHandler(value = StatusException.class)
    public String findUserInfoPage(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model, HttpSession session) {
        VipCard vipCard = new VipCard();
        vipCard.setState(1);
        List<VipCard> vipCards = vipCardService.findVipCard(vipCard);
        session.setAttribute("vipCards", vipCards);
        System.out.println("---------------分页--------------------进入获得用户信息控制层");
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5); //传入当前页

        UserInfo userInfo = new UserInfo();
        SystemAdminAuthorizingRealm.Principal employee = AdminUserUtils.getPrincipal();
        Employee et = AdminUserUtils.getPrincipal().getEmployee();
        userInfo.setState(1);
        if (et.getEmTypeId() > 2){
        userInfo.setBrid(et.getBranchesId());
        }
        List<UserInfo> userInfoList = userInfoService.findAll(userInfo);
        for (int i =0;i<userInfoList.size();i++){
            for (int j =0;j<vipCards.size();j++){
                if (userInfoList.get(i).getVipId()== vipCards.get(j).getVipId()){
                    userInfoList.get(i).setCateName(vipCards.get(j).getVipName());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(userInfoList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "userInfoEntry";
    }

    @RequiresPermissions("user")
    @RequestMapping("/findUserInfoAuth")
    @ExceptionHandler(value = StatusException.class)
    public String findUserInfoPageByAuth(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model, HttpSession session) {
        VipCard vipCard = new VipCard();
        vipCard.setState(1);
        List<VipCard> vipCards = vipCardService.findVipCard(vipCard);
        session.setAttribute("vipCards", vipCards);
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5); //传入当前页

        UserInfo userInfo = new UserInfo();
        Employee et = AdminUserUtils.getPrincipal().getEmployee();
        userInfo.setState(1);
        if (et.getEmTypeId() > 2){
            userInfo.setBrid(et.getBranchesId());
        }
        List<UserInfo> userInfoList = userInfoService.findAll(userInfo);
        for (int i =0;i<userInfoList.size();i++){
            for (int j =0;j<vipCards.size();j++){
                if (userInfoList.get(i).getVipId()== vipCards.get(j).getVipId()){
                    userInfoList.get(i).setCateName(vipCards.get(j).getVipName());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(userInfoList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "userInfoEntryAuth";
    }

    /**
     * 模糊查询
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/searchUserInfo")
    @ExceptionHandler(value = StatusException.class)
    public String searchUserInfo(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                 @RequestParam(value = "type") String type,
                                 @RequestParam(value = "content") String content,
                                 Model model) {
        System.out.println("---------------分页--------------------进入模糊查询");
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        PageHelper.startPage(pageNo, 5);
        UserInfo userInfo = null;
        List<UserInfo> userInfoList = null;
        switch (type) {
            case "userName":
                //用户名
                userInfo = new UserInfo();
                userInfo.setUserName(content);
                userInfoList = userInfoService.searchUserInfo(userInfo);
                break;
            default:
                break;
        }

        //分页
        PageInfo pageInfo = new PageInfo(userInfoList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("content", content);
            model.addAttribute("type", type);
        }
        return "userInfoEntry";

    }

    @RequestMapping("/getallInfo")
    @ResponseBody
    public Object getAllUserInfo(@RequestParam("uid") String uid){
        Map<String,Object> map = new HashMap<>();
            Integer userid =null;
            try {
                userid = Integer.valueOf(uid);
            }catch (Exception e){
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userid);
            userInfo = userInfoService.findOne(userInfo);
            if (userInfo != null){
                map.put("userInfo",userInfo);
                VipInfo vipInfo = new VipInfo();
                vipInfo.setUserId(userid);
                vipInfo = vipInfoService.findOne(vipInfo);
                if (vipInfo!=null){
                    map.put("vipInfo",vipInfo);
                    VipCard vipCard = new VipCard();
                    vipCard.setVipId(vipInfo.getVipId());
                    vipCard =vipCardService.findOne(vipCard);
                    if (vipCard!= null){
                        map.put("vipCard",vipCard);
                    }
                    Branches branches = new Branches();
                    branches.setBranId(vipInfo.getBranId());
                    branches = branchesService.findOneBranches(branches);
                    if (branches !=null){
                        map.put("branches",branches);
                    }
                }
                BabyInfo babyInfo = new BabyInfo();
                babyInfo.setBabyId(userInfo.getBabyId());
                babyInfo = babyInfoService.findOne(babyInfo);
                if (babyInfo != null){
                    map.put("babyInfo",babyInfo);
                }
            }
        return map;
    }

}
